<?
include_once $_SERVER['DOCUMENT_ROOT'] . "/library/phpqrcode/qrlib.php";
include_once $_SERVER['DOCUMENT_ROOT'] . "/conf/config.php";

$dbconn = @mysql_connect($dbhost, $dbuser, $dbpass);
@mysql_select_db($dbname, $dbconn);

// 다음 지도 API 쿼리 스트링
$request = 'http://apis.daum.net/maps/addr2coord?apikey=' . $daum_key . '&output=json&q=';

// 요청을 수행합니다.
$map_request = $request . urlencode($_POST['txt_oldsi'] . " " . $_POST['txt_oldgu'] . " " . $_POST['txt_olddong'] . " " . $_POST['txt_oldbunji']);

if(!$map_request) {
	echo json_encode('DAUM 지도 서비스 에러');
	exit();
}
// 좌표값을 처리한다.
$map_result = json_decode(file_get_contents($map_request), true);

// 최대 SEQNO 값을 구한다.
$seqQRY = "select max(SEQNO) as SEQNO from qr_data";
$result = @mysql_fetch_array(@mysql_query($seqQRY, $dbconn));
$SEQNO = $result['SEQNO']+1;

// QR Code 생성
$file_name  = str_replace(" ", ".", trim($_POST['txt_newsi'])) . ".";
$file_name .= str_replace(" ", ".", trim($_POST['txt_newgu'])) . ".";
$file_name .= str_replace(" ", ".", trim($_POST['txt_newdong'])) . ".";
$file_name .= str_replace(" ", ".", trim($_POST['txt_newbunji'])) . ".png";
$file_name  = str_replace("..", ".", $file_name);

QRcode::png($code_key.$SEQNO, $_SERVER['DOCUMENT_ROOT'].'/'.$code_loc.$file_name, 'H', 4, 2);

// 데이터베이스 삽입 쿼리 스트링
$insQRY  = "insert into qr_data values ('" . $SEQNO . "','";
$insQRY .= $_POST['txt_oldsi'] . "','" . $_POST['txt_oldgu'] . "','" . $_POST['txt_olddong'] . "','" . $_POST['txt_oldbunji'] ."','";
$insQRY .= $_POST['txt_newsi'] . "','" . $_POST['txt_newgu'] . "','" . $_POST['txt_newdong'] . "','" . $_POST['txt_newbunji'] ."','";
$insQRY .= $_POST['txt_used'] . "','" . $_POST['txt_uint'] . "','" . $_POST['txt_cpic'] . "','" . $_POST['txt_cdate'] ."','";
$insQRY .= $map_result[channel][item][0][lat] . "','" . $map_result[channel][item][0][lng] . "','" . $file_name . "','','','','','','','','','','')";

if(@mysql_query($insQRY, $dbconn)) echo json_encode('success');
else echo json_encode('데이터베이스 입력 에러');
?>